#coding:utf-8
import scrapy
import json
import csvout

class QuotesSpider(scrapy.Spider):
    name = "wanfang"

    def start_requests(self):
        url = 'http://s.wanfangdata.com.cn'
        tag = getattr(self, 'tag', None)

        if tag is not None:
            url = url + '/Paper.aspx?q=' + tag + '&f=top'
            yield scrapy.Request(url, callback=self.parse)

    def parse(self, response):
        #从chrome里拷出来的xpath
        res = response.xpath('/html/body/div[2]/div[1]/div[1]/span/text()').extract_first()
        #过滤出其中的数字
        num = filter(lambda x: x.isdigit(), res)
        num = int(num)
        outdic = {
            'wanfang_num': num
        }
        outli = ['wanfang_num', num, response.url]
        csvout.list2csv(outli)
        yield outdic

